#include<iostream>
#include "Parser.h"
#include<sstream>

using namespace std;

string toUpper(string str);
bool intjudge(string suspect);

Parser::Parser()
{
    Arg* help = new Arg();
    help->name = "--help";
    //help->value = ;
    help->info = "show this help message and exit";

    Args.push_back(help);
}

void Parser::add_argument(string argName, int argValue, string argInfo)
{
    Arg* arg = new Arg();
    arg->name = argName;
    arg->value = argValue;
    arg->info = argInfo;

    Args.push_back(arg);
}

void Parser::init(string input)
{
    if(input.find("--help")!=string::npos)
    {
        Help();
        exit(0);
    }

    stringstream resource;
    resource << input;//将传入的字符串转为字符串流

    if(input == "\n")
    {
        return;
    }
    
    //如果没有help，开始一对一对读取
    while(!resource.eof())
    {
        string name,value;//用于存放从input中读取的单个字符串
        bool argExist = false;//用于判断命令是否存在
        int argIndex = -1;//用于记录对应命令在vector中的位置

        resource >> name;

        //找命令
        for(int i=1;i<Args.size();i++)
        {
            Arg* arg = Args[i];
            if(arg->name == name)
            {
                argExist = true;
                argIndex = i;
            }
        }

        if(argExist == false)
        {
            Unrecognized(name);
            exit(0);
        }
        else if (argExist == true)
        {
            if(resource.eof())
            {
                Expected(name);
                exit(0);
            }
            else if(!resource.eof())
            {
                resource >> value;
                if(!intjudge(value))
                {
                    Invalid(name,value);
                    exit(0);
                }
                else if(intjudge(value))
                {
                    istringstream num(value);
                    num >> Args[argIndex]->value;
                }
            }
        }
    }
}

int Parser::get_argument(string argName)
{
    string argname = string("--") + string(argName);
    for(int i=1;i<Args.size();i++)
    {
        Arg* arg = Args[i];
        if(arg->name == argname)
            return arg->value;
    }
}


//辅助代码块
#pragma region 
void Parser::Help()
{
    cout<<"usage: ./main [--help]";
    for(int i=1;i<Args.size();i++)
    {
        Arg* arg = Args[i];
        cout<<" ["<<arg->name<<" "<<toUpper((arg->name).substr(2))<<"]";//注意最后一个测试用的arg->value记得删掉
    }
    cout<<"\n";
    cout<<"\noptional arguments:\n";
    cout<<"--help   show this help message and exit\n";//偷懒，这一行应该是不会变的
    for(int i=1;i<Args.size();i++)
    {
        Arg* arg = Args[i];
        cout<<arg->name<<" "<<toUpper((arg->name).substr(2))<<"   "<<arg->info<<endl;
    }
}


string toUpper(string str)
{
    string result = str;
    for (char& c : result)
    {
        c = toupper(c); // 将字符转换为大写形式
    }
    return result;
}

void Parser::Unrecognized(string stranger)
{
    cout<<"usage: ./main [--help]";
    for(int i=1;i<Args.size();i++)
    {
        Arg* arg = Args[i];
        cout<<" ["<<arg->name<<" "<<toUpper((arg->name).substr(2))<<"]";
    }
    cout<<"\n";
    cout<<"./main: error: unrecognized arguments: "<<stranger<<endl;
}

void Parser::Expected(string beggar)
{
    cout<<"usage: ./main [--help]";
    for(int i=1;i<Args.size();i++)
    {
        Arg* arg = Args[i];
        cout<<" ["<<arg->name<<" "<<toUpper((arg->name).substr(2))<<"]";
    }
    cout<<"\n";
    cout<<"./main: error: argument "<<beggar<<": expected an argument"<<endl;
}

bool intjudge(string suspect)
{
    for(char& c:suspect)
    {
        if(!isdigit(c))
            return false;
    }
    return true;
}

void Parser::Invalid(string criminal, string element)
{
    cout<<"usage: ./main [--help]";
    for(int i=1;i<Args.size();i++)
    {
        Arg* arg = Args[i];
        cout<<" ["<<arg->name<<" "<<toUpper((arg->name).substr(2))<<"]";
    }
    cout<<"\n";
    cout<<"./main: error: argument "<<criminal<<": invalid int value: '"<<element<<"'"<<endl;
}
#pragma endregion